/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lcfgly.Dao;

import com.lcfgly.Database.Message;
import com.lcfgly.DatabaseFactory.DataFactoryInterface;
import com.lcfgly.Interface.GetCacheMark;
import com.lcfgly.Interface.UpdateCacheMark;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

/**
 *
 * @author lcfgly
 */
@Repository
public class MessageImpl implements Serializable{
    
    @Autowired
        @Qualifier("databaseshoprice")
    private DataFactoryInterface dataFactory;
    
    private final String sqlstart="SELECT id,uptime,comeid,goid,message,back,opend,backtime FROM ";
   
    /**
     * 分页未回复的消息列表
     * @param to 
     * @param page
     * @return
     * @throws SQLException 
     */
    public Map GetNotBackMessage(int to,int page)throws SQLException{
        String sql=sqlstart+getmessagetable(to)+" WHERE goid=? And back is null Order By uptime desc";
        String s="SELECT count(*) FROM "+getmessagetable(to)+" WHERE goid=? And back is null";
        int rownum=dataFactory.Getrowsnum(s, to);
        return dataFactory.Page(Message.class, sql, rownum, page, 20, to);
    }
    
    /**
     * 分页已回复的消息列表
     * @param to
     * @param page
     * @return
     * @throws SQLException 
     */
    public Map GetBackMessage(int to,int page) throws SQLException{
       String sql=sqlstart+getmessagetable(to)+" WHERE goid=? And back is not null  Order By uptime desc ";
       String s="SELECT count(*) FROM "+getmessagetable(to)+" WHERE goid=? And back is not null";
       int rownum=dataFactory.Getrowsnum(s, to);
       return dataFactory.Page(Message.class, sql, rownum, page, 20, to);
    }
    
    /**
     * 更新数据
     * @param arg0
     * @return
     * @throws SQLException 
     */
    public int update(Message arg0)throws SQLException{
        return dataFactory.update(getmessagetable(arg0.getGoid()), arg0, "id=?", arg0.getId());
    }
    
    /**
     * 插入数据
     * @param arg0
     * @return
     * @throws SQLException 
     */
    @UpdateCacheMark("GetMessage_${0.goid}_1")
    public int insert(Message arg0)throws SQLException{
        return dataFactory.insert(arg0, getmessagetable(arg0.getGoid()));
    }
    
    /**
     * 删除消息
     * @param id
     * @param to
     * @return
     * @throws SQLException 
     */
    public int delete(int id,int to)throws SQLException{
        return dataFactory.delete(getmessagetable(to), "id=?", id);
    }
    
    /**
     * 确定消息数据表
     * @param userid
     * @return 
     */
    public String getmessagetable(int userid){
       return "shop_message"+(userid&3)+" ";
    }
    
    /**
     * 按id查找数据
     * @param id
     * @param to
     * @return
     * @throws SQLException 
     */
    public Message findById(int id,int to)throws SQLException{
        String sql=sqlstart+getmessagetable(to)+" WHERE id=? AND goid=?";
        return dataFactory.one(Message.class, sql, id,to);
    }
    
    /**
     * 获取所有的评论过
     * @param to
     * @param page
     * @return
     * @throws SQLException 
     */
    @GetCacheMark("GetMessage_${0}_${1}")
     public Map GetMessage(int to,int page) throws SQLException{
       String sql=sqlstart+getmessagetable(to)+" WHERE goid=?  Order By uptime desc ";
       String s="SELECT count(*) FROM "+getmessagetable(to)+" WHERE goid=?";
       int rownum=dataFactory.Getrowsnum(s, to);
       return dataFactory.Page(Message.class, sql, rownum, page, 20, to);
    }
    
     /**
      * 首页评论展示
      * @param to
      * @return
      * @throws SQLException 
      */
     @GetCacheMark("IndexGetMessage_${0}")
     public Map IndexGetMessage(int to)throws SQLException{
        String sql=sqlstart+getmessagetable(to)+" WHERE goid=?  Order By uptime desc ";
        String s="SELECT count(*) FROM "+getmessagetable(to)+" WHERE goid=?";
        int rownum=dataFactory.Getrowsnum(s, to);
        return dataFactory.Page(Message.class, sql, rownum, 1, 10, to);
     }
     
}
